import java.util.Arrays;

/**
 * @author ASUS
 * @date 2020/5/20
 */
public class 冒泡排序 {

    public static void main(String[] args) {
        int[] arr = {3, 2, 4, 0, 5, 7, 1, 8, 9};

        //冒泡
        //bubblSort(arr);


        //快速排序
        quicrsort(arr, 0, arr.length - 1);

        System.out.println(Arrays.toString(arr));

//        //二分查找法
//        int result = binarySerach(arr, 8);

       /* System.out.println(result);
*/

    }

    /** 二分查找
     * @param arr
     * @param value
     * @return
     */
    private static int binarySerach(int[] arr, int value) {

        int head =0,foot=arr.length -1;

        while ( head <= foot) {
            int mid = (head + foot) / 2;
            if ( arr[mid] < value) {
                head = mid + 1;
            }else if (arr[mid] > value) {
                foot =head -1;
            }else {
                return mid;
            }

        }
        return -1;


    }

    /** 快速排序
     * @param arr
     * @param left
     * @param right
     * @return
     */
    private static int[] quicrsort(int[] arr, int left, int right) {

        if (left < right) {
            int i=left,j=right,targe=arr[left];
            while (i<j) {
                while (i<j && arr[j] > targe) {
                    j--;
                }
                if (i<j) {
                    arr[i++]=arr[j];
                }
                while (i<j && arr[i] <targe){
                    i++;
                }
                if (i<j) {
                    arr[j--]=arr[i];
                }
            }

            arr[i] = targe;
            quicrsort(arr,left,i-1);
            quicrsort(arr,i+1,right);
        }
        return arr;

    }

    private static void bubblSort(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

    }

}
